#!/usr/bin/env python
#
# def.py  - defs for SdrScript python layer# 
# User: bob
# Date: 11/05/14
# Copyright (C) 2014 by G3UKB Bob Cowdery
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#    
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#    
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#    
#  The author can be reached by email at:   
#     bob@bobcowdery.plus.com
#

#===============================================================================
# Usable frequency span
MIN_FREQ = 0.1
MAX_FREQ = 60.0

#===============================================================================
# Logging

CLOSE = 'close'
HIDE = 'hide'
SHOW = 'show'
LOG = 'log'     #output to log only
TRANS = 'trans' #output to transcript only
BOTH = 'both'   #output to both

#===============================================================================
# Publish/subscribe event types
TX_FREQ_EVNT    = 'evtTxFreq'
RX_0_FREQ_EVNT  = 'evtRx0Freq'
RATE_EVNT       = 'evtRate'
NUM_RX_EVNT     = 'evtNumRx'
BLK_SZ_EVNT     = 'evtBlkSz'
DUPLEX_EVNT     = 'evtDuplex'
TX_ENABLE_EVNT  = 'evtTxEnable'
AUDIO_CH_EVNT   = 'evtAudioChange'
ANTENNA_EVNT    = 'evtAntenna'
DISPLAY_EVNT    = 'evtDisplay'
RESET_EVNT      = 'evtReset'

#===============================================================================
# Constants used in UI and saved state

#=============================
# Window settings
WINDOW_X = 'X'
WINDOW_Y = 'Y'
WINDOW_WIDTH = 'window_width'
WINDOW_MIN_WIDTH = 'window_min_width'
WINDOW_HEIGHT = 'window_height'
DISPLAY_WIDTH = 'display_width'
DISPLAY_HEIGHT = 'display_height'

#=================================================================
# Configuration settings
# Top level tags
COMMON = 'common'
RX = 'rx'
TX = 'tx'

#=============================
# Common settings
IN_RATE = 'inrate'
OUT_RATE = 'outrate'
NUM_RX = 'num_rx'
IQ_BLK_SZ = 'iq_blk_sz'
MIC_BLK_SZ = 'mic_blk_sz'
DUPLEX = 'duplex'
TX = 'tx'

#=============================
# Power settings
POWER = 'power'
PWR_MODE = 'pwr_mode'
PWR_IP = 'pwr_ip'
PWR_USER = 'pwr_user'
PWR_PW = 'pwr_pw'
PWR_OUTLET = 'pwr_outlet'

#=============================
# Audio settings
AUDIO = 'audio'
HPSDR = 'HPSDR'
LOCAL = 'Local'
IN_SRC = 'in_src'
IN_HOSTAPI = 'in_hostapi'
IN_DEV = 'in_dev'
OUT_SINK = 'out_sink'
ROUTING = 'routing'
LEFT = 'Left'
RIGHT = 'Right'
BOTH = 'Both'

#=============================
# Antenna settings
ANTENNA = 'antenna'
ANT_SETTING = 'ant_settings'
ANT_PREC = 'ant_prec'
ANT_1 = 'ant1'
ANT_2 = 'ant2'
ANT_3 = 'ant3'
RX_1 = 'rx1'
RX_2 = 'rx2'
RX_3 = 'rx3'
ANT_DEFAULT = 'ant_default'
ANT_ANT = 'ant_ant'
ANT_ANTRX = 'ant_antrx'

#=============================
# Transmitter settings
TX = 'tx'
LEVELS = 'levels'
TUNE_DRIVE_LEVEL = 'tune_drive'

#=============================
# Receiver settings
RX = 'rx'
DISPLAYS = 'displays'
PAN_MODE = 'pan_mode'
PAN_FRM_AV = 'pan_frame_av'
WINDOW_TYPE = 'window_type'
PAN_FFT_SZ = 'fft_sz'

# End Configuration settings
#=================================================================

#=============================
# DSP settings
FREQ = 'freq'
MODE = 'mode'
FILTER = 'filter'
AGC = 'agc'
GAIN = 'gain'
    
#=============================
# Preamp and attenuator settings
PREAMP_HF = 'preamp_hf'
PREAMP_6M = 'preamp_6m'
ATTN = 'attenuator'
ATTN_NONE = 0
ATTN_10 = 10
ATTN_20 = 20
ATTN_30 = 30

attn_lookup = (
        (ATTN_NONE, 'OFF'),
        (ATTN_10, '10dB'),
        (ATTN_20, '20dB'),
        (ATTN_30, '30dB')       
)
PRE_OFF = 0
PRE_ON = 1

pre_lookup = (
        (PRE_OFF, 'OFF'),
        (PRE_ON, 'ON')      
)

#=============================
# RX Settings
# RX meter settings
M_RX_S_PEAK = 0
M_RX_S_AV = 1
M_RX_IN_PEAK = 2
M_RX_IN_AV = 3
M_RX_AGC_GAIN= 4
M_RX_AGC_PEAK = 5
M_RX_AGC_AV = 6

#=============================
# TX Settings
# TX meter settings
M_TX_MIC_PK = 0
M_TX_MIV_AV = 1
M_TX_EQ_PK = 3
M_TX_EQ_AV = 4
M_TX_LEV_PK = 5
M_TX_LEV_AV = 6
M_TX_LEV_GAIN = 7
M_TX_COMP_PK = 8
M_TX_COMP_AV = 9
M_TX_ALC_PK = 10
M_TX_ALC_AV = 11
M_TX_ALC_GAIN = 12
M_TX_OUT_PK = 13
M_TX_OUT_AV = 14

#===============================================================================
# Metis

# Metis has a fixed port number it communicates on
METIS_PORT = 1024
# Use a 5s timeout for all Metis communications
METIS_TIMEOUT = 5

# Action bytes
METIS_DISCOVER = 0x02
METIS_START = 0x04
METIS_START_IQ = 0x01
METIS_START_ALL = 0x03
METIS_STOP = 0x04
METIS_STOP_ALL = 0x00

# Block sizes
METIS_BLK_SZ = 1032
SMPL_FRM_SZ = 504
SYNC_SZ = 3
CTRL_SZ = 5

# Offsets into Metis data block
ENDPOINT = 3
SEQ_ST = 4
SEQ_END = 8
# First block
SYNC_0_ST = SEQ_END
SYNC_0_END = SYNC_0_ST + SYNC_SZ
CTRL_0_ST = SEQ_END + SYNC_SZ
CTRL_0_END = CTRL_0_ST + CTRL_SZ
FRM_0_ST = CTRL_0_END
FRM_0_END = FRM_0_ST + SMPL_FRM_SZ
# Second block
SYNC_1_ST = FRM_0_END
SYNC_1_END = SYNC_1_ST + SYNC_SZ
CTRL_1_ST = FRM_0_END + SYNC_SZ
CTRL_1_END = CTRL_1_ST + CTRL_SZ
FRM_1_ST = CTRL_1_END
FRM_1_END = FRM_1_ST + SMPL_FRM_SZ

# End points
EP4 = 0x04	# WBS
EP6 = 0x06	# IQ

# CC output values for C0
CC_OUT_GEN = 0
CC_OUT_GEN_SET = 0x00
CC_OUT_NCO_1 = 1
CC_OUT_NCO_1_SET = 0x02
CC_OUT_NCO_2 = 2
CC_OUT_NCO_2_SET = 0x04
CC_OUT_NCO_3 = 3
CC_OUT_NCO_3_SET = 0x06
CC_OUT_NCO_4 = 4
CC_OUT_NCO_4_SET = 0x08
CC_OUT_NCO_5 = 5
CC_OUT_NCO_5_SET = 0x0A
CC_OUT_NCO_6 = 6
CC_OUT_NCO_6_SET = 0x0C
CC_OUT_NCO_7 = 7
CC_OUT_NCO_7_SET = 0x0E
CC_OUT_NCO_8 = 8
CC_OUT_NCO_8_SET = 0x10
CC_OUT_AUX_1 = 9
CC_OUT_AUX_1_SET = 0x12
CC_OUT_AUX_2 = 10
CC_OUT_AUX_2_SET = 0x14

#===============================================================================
# DSP

# Max instances
MAX_TX = 1
MAX_RX = 7

# Channel type
CH_RX = 0
CH_TX = 1
CH_MON = 2

# Channel run state and state switch transition 
CH_STATE_STOP = 0
CH_STATE_START = 1
CH_TRANSITION_WAIT = 1
CH_TRANSITION_NOWAIT = 0

# RX modes
CH_LSB = 0
CH_USB = 1
CH_DSB = 2
CH_CWL = 3
CH_CWU = 4
CH_FM = 5
CH_AM = 6
CH_DIGU = 7
CH_SPEC = 8
CH_DIGL = 9
CH_SAM = 10
CH_DRM = 11

mode_lookup = (
        (CH_LSB, 'LSB'),
        (CH_USB, 'USB'),
        (CH_DSB, 'DSB'),
        (CH_CWL, 'CW-L'),
        (CH_CWU, 'CW-U'),
        (CH_FM, 'FM'),
        (CH_AM, 'AM'),
        (CH_DIGU, 'DIG-U'),
        (CH_SPEC, 'SPEC'),
        (CH_DIGL, 'DIG-L'),
        (CH_SAM, 'SAM'),
        (CH_DRM, 'DRM'),
)

# RX filter
CH_BP_OFF = 0
CH_BP_ON = 1
CH_BP_WIN_4_TERM = 0
H_BP_WIN_7_TERM = 1

# Filters
CH_6K0 = 0
CH_4K0 = 1
CH_2K7 = 2
CH_2K4 = 3
CH_2K1 = 4
CH_1K0 = 5
CH_500 = 6
CH_250 = 7
CH_100 = 8

filter_lookup = (
        (CH_6K0, 100, 6100, '6.0 KHz'),
        (CH_4K0, 100, 4100, '4.0 KHz'),
        (CH_2K7, 300, 3000, '2.7 KHz'),
        (CH_2K4, 300, 2700, '2.4 KHz'),
        (CH_2K1, 300, 2400, '2.1 KHz'),
        (CH_1K0, 300, 1300, '1.0 KHz'),
        (CH_500, 500, 1000, '500 Hz'),
        (CH_250, 600, 850, '250 Hz'),
        (CH_100, 700, 800, '100 Hz')
)

# AGC mode
CH_AGC_OFF = 0
CH_AGC_LONG = 1
CH_AGC_SLOW = 2
CH_AGC_MED = 3
CH_AGC_FAST = 4

agc_lookup = (
        (CH_AGC_OFF, 'OFF'),
        (CH_AGC_LONG, 'LONG'),
        (CH_AGC_SLOW, 'SLOW'),
        (CH_AGC_MED, 'MED'),
        (CH_AGC_FAST, 'FAST')       
)

# Panadapter 
# Averaging mode
PAN_PEAK = -1
PAN_NONE = 0
PAN_TIME_AV_LIN = 1
PAN_TIME_AV_LOG = 2
PAN_WIN_AV_LIN = 3
PAN_WIN_AV_LOG = 4
PAN_LNF_LIN = 5
PAN_LNF_LOG = 6

pan_mode_lookup = (
        (PAN_PEAK, 'Peak Detect'),
        (PAN_NONE, 'No Average'),
        (PAN_TIME_AV_LIN, 'Time Average Linear'),
        (PAN_TIME_AV_LOG, 'Time Average Log'),
        (PAN_WIN_AV_LIN, 'Window Average Linear'),
        (PAN_WIN_AV_LOG, 'Window Average Log'),
        (PAN_LNF_LIN, 'Low Noise Floor Linear'),
        (PAN_LNF_LOG, 'Low Noise Floor Log')
)

# Window types
RECTANGULAR = 0
BLACKMAN_HARRIS = 1
HANN = 2
FLAT_TOP = 3
HAMMING = 4
KAISER = 5

window_lookup = (
        (RECTANGULAR, 'Rectangular'),
        (BLACKMAN_HARRIS, 'Blackman-Harris'),
        (HANN, 'Hann'),
        (FLAT_TOP, 'Flat-top'),
        (HAMMING, 'Hamming'),
        (KAISER, 'Kaiser')
)
